package com.mifengkong.kotlincommon.recyclerview.refreshload.load

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import com.mifengkong.kotlincommon.R

/**
 * Created by jack on 2018/6/20
 */
class DefaultLoadingView : LoadViewCreator() {

    private lateinit var tvFooter: TextView
    private lateinit var progressBar: ProgressBar

    private var isNeedRelease = true

    override fun getLoadView(context: Context, parent: ViewGroup): View {
        val loadMoreView = LayoutInflater.from(context).inflate(R.layout.default_loadmore_footer, parent, false)
        tvFooter = loadMoreView.findViewById(R.id.default_loadmore_footer_tv)
        progressBar = loadMoreView.findViewById(R.id.default_loadmore_footer_progressbar)
        return loadMoreView
    }

    override fun onPull(currentDragHeight: Int, loadViewHeight: Int, currentLoadStatus: Int) {
        val rotate = currentDragHeight.toFloat() / loadViewHeight
        progressBar.visibility = View.GONE
        if (rotate >= 1 && currentLoadStatus == PullUpLoadingRecyclerView.LOAD_STATUS_LOOSEN_LOADING && isNeedRelease) {
            isNeedRelease = false
            tvFooter.text = "松开加载更多..."
        } else if (rotate < 1 && currentLoadStatus == PullUpLoadingRecyclerView.LOAD_STATUS_PULL_DOWN_REFRESH && !isNeedRelease) {
            isNeedRelease = true
            tvFooter.text = "上拉加载更多"
        }
    }

    override fun onLoading() {
        progressBar.visibility = View.VISIBLE
        tvFooter.text = "正在加载更多..."
    }

    override fun onStopLoad() {
        progressBar.visibility = View.GONE
        tvFooter.text = "上拉加载更多"
    }

    override fun onNoMoreData() {
        tvFooter.text = "——— 我是有底线的 ———"
    }

}